package com.xiashitech.agent.instrumentation.base;

import com.xiashitech.agent.instrumentation.log.XSLevel;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import io.opentelemetry.javaagent.shaded.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.javaagent.shaded.io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.javaagent.shaded.io.opentelemetry.context.Context;
import org.jetbrains.annotations.Nullable;

import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.logging.Level.*;

public abstract class AbstractAttributesExtractor<REQUEST, RESPONSE> implements AttributesExtractor<REQUEST, RESPONSE> {
    protected static final Logger logger = Logger.getLogger("SpanAttributesExtractor");
    @Override
    public void onStart(AttributesBuilder attributesBuilder, Context context, REQUEST request) {
        if(InstrumentationConfig.get().getBoolean("xs.debug",false)) {
            logger.log(XSLevel.DEBUG, getAttributesExtractorName()+" - start - "+context.toString());
        }
        onXSStart(attributesBuilder,context,request);
    }
    public abstract void onXSStart(AttributesBuilder attributesBuilder, Context context, REQUEST request);
    @Override
    public void onEnd(AttributesBuilder attributesBuilder, Context context, REQUEST request, @Nullable RESPONSE response, @Nullable Throwable throwable) {
        onXSEnd(attributesBuilder,context,request,response,throwable);
        if(InstrumentationConfig.get().getBoolean("xs.debug",false)) {
            logger.log(XSLevel.DEBUG, getAttributesExtractorName()+" - end - "+context.toString());
        }
    }
    public abstract void onXSEnd(AttributesBuilder attributesBuilder, Context context, REQUEST request, RESPONSE response, Throwable throwable);
    public abstract String getAttributesExtractorName();
}
